package com.fingard.dsp.bank.directbank.icbc03;

import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IContract;
import com.fingard.dsp.bank.reqrespobj.contract.*;
import com.fingard.dsp.bank.reqrespobj.contract.detail.RetAtsContractDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;


public class ICBC03Contract extends ICBC03Base implements IContract {
    @Override
    public RetAtsContract contract(ReqContract p_req) throws Exception {
        RetAtsContract retAts = new RetAtsContract(p_req.reqHeader);
        RetAtsContractDetail tmpRetDetail = new RetAtsContractDetail();
        tmpRetDetail.reqSeqID = p_req.contractDetail.askNumber;
        try{
            ActSetItem actItem = getBankConfig().getActSetItem(p_req.contractDetail.recAct, "contract");
            if (actItem == null) {
                throw new Exception("未找到账号" + p_req.contractDetail.recAct + "对应的商户配置信息");
            }
            XmlTextWriter xmlWriter = new XmlTextWriter();
            JSONObject data = new JSONObject();
            data.put("app_id",actItem.appId);
            if ("01".equals(p_req.contractDetail.step)) {
                txCode = "ECIP000090";
                reqSeqID = p_req.contractDetail.askNumber;
                writeCommonHeader(xmlWriter);
                JSONObject bizContent = new JSONObject();
                bizContent.put("merchant_id",actItem.merchantId);
                bizContent.put("merchant_type","1");
                bizContent.put("mobile",p_req.contractDetail.cellPhone);
                bizContent.put("cardno",p_req.contractDetail.payAct);
                bizContent.put("shop_name","1");
                data.put("bizContent",bizContent);
            } else if ("02".equals(p_req.contractDetail.step)) {
                txCode = "ECIP000091";
                reqSeqID = p_req.reqHeader.transSeqID;
                writeCommonHeader(xmlWriter);
                JSONObject bizContent = new JSONObject();
                bizContent.put("merchant_id",actItem.merchantId);
                bizContent.put("sign_validity_period","");
                bizContent.put("external_agreement_no",p_req.contractDetail.sourceNoteCode);
                bizContent.put("merchant_acct",actItem.corpCode);
                bizContent.put("merchant_type","1");

                bizContent.put("cvn2",p_req.contractDetail.cvv2);
                bizContent.put("mobile",p_req.contractDetail.cellPhone);
                bizContent.put("cardno",p_req.contractDetail.payAct);
                bizContent.put("expired",p_req.contractDetail.expiredDate);
                bizContent.put("cust_name",p_req.contractDetail.payName);
                bizContent.put("cust_cert_type",convertCertTypeToBank(p_req.contractDetail.certType));
                bizContent.put("cust_cert_no",p_req.contractDetail.certNum);

                bizContent.put("message_flag","1");
                bizContent.put("ums_msg_no",p_req.contractDetail.token);
                bizContent.put("ums_verify_code",p_req.contractDetail.smsCode);
                data.put("bizContent",bizContent);
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "未能识别的step：" + p_req.contractDetail.step;
                retAts.writeRetDetail(tmpRetDetail);
                return retAts;
            }
            xmlWriter.writeElementString("Transaction_Body",JSONObject.toJSONString(data));
            xmlWriter.writeEndDocument();//Transaction_Body
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String returnMsg = tmpBkReader.getFirstText("/TX/TX_BODY/ENTITY/Transaction_Body");
                JSONObject retObj = JSONObject.parseObject(returnMsg);
                tmpRetDetail.respCode = "0";
                if(!"0".equals(retObj.getString("returnCode"))){
                    tmpRetDetail.respCode = "-1";
                }
                tmpRetDetail.respInfo = retObj.getString("returnMsg");
                if("01".equals(p_req.contractDetail.step)) {
                    tmpRetDetail.token = retObj.getString("umsMsgNo");
                    tmpRetDetail.signConfirmType = "2";
                }else {
                    if("0".equals(tmpRetDetail.respCode)){
                        tmpRetDetail.signState = ContractState.SUCCESS;
                    } else {
                        tmpRetDetail.signState = ContractState.FAIL;
                    }
                    tmpRetDetail.authCode = retObj.getString("agreementNo");
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpSbMsg.toString();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsContract upContract(ReqContract p_req) throws Exception {
        return null;
    }

    @Override
    public RetAtsContract qryContract(ReqContract p_reqQry) throws Exception {
        RetAtsContract retAts = new RetAtsContract(p_reqQry.reqHeader);
        RetAtsContractDetail tmpRetDetail = new RetAtsContractDetail();
        tmpRetDetail.reqSeqID = p_reqQry.contractDetail.askNumber;
      try{
        ActSetItem actItem = getBankConfig().getActSetItem(p_reqQry.contractDetail.recAct, "contract");
        if (actItem != null) {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            JSONObject data = new JSONObject();
            data.put("app_id",actItem.appId);
            txCode = "ECIP000092";
            reqSeqID = getQryTransID();
            writeCommonHeader(xmlWriter);
            JSONObject bizContent = new JSONObject();
            bizContent.put("merchant_id",actItem.merchantId);
            bizContent.put("merchant_type","1");
            bizContent.put("consumer_id","baoxian");
            bizContent.put("external_agreement_no",p_reqQry.contractDetail.sourceNoteCode);
            bizContent.put("agreement_no",p_reqQry.contractDetail.authCode);
            bizContent.put("query_type","3");
            bizContent.put("cardno",p_reqQry.contractDetail.payAct);
            data.put("bizContent",bizContent);
            xmlWriter.writeElementString("Transaction_Body",JSONObject.toJSONString(data));
            xmlWriter.writeEndDocument();//Transaction_Body
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String returnMsg = tmpBkReader.getFirstText("/TX/TX_BODY/ENTITY/Transaction_Body");
                JSONObject retObj = JSONObject.parseObject(returnMsg);

                tmpRetDetail.respCode = "0";
                if("0".equals(retObj.getString("returnCode"))){
                    tmpRetDetail.respInfo = retObj.getString("returnMsg");
                    String count_nums = retObj.getString("countNums");
                    if("1".equals(count_nums)){
                        tmpRetDetail.signState = ContractState.SUCCESS;
                    }else {
                        tmpRetDetail.signState = ContractState.UNSIGNED;
                    }
                    tmpRetDetail.signConfirmType = "2";
                }else if("2".equals(retObj.getString("returnCode"))){
                    tmpRetDetail.respCode = "0";
                    tmpRetDetail.signState = ContractState.UNSIGNED;
                    tmpRetDetail.respInfo = "未签约";
                }else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = retObj.getString("returnMsg");
                }

            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpSbMsg.toString();
            }
        } else {
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = "未找到账号:[" + p_reqQry.contractDetail.recAct + "]商户配置信息!";
        }
      } catch (Exception ex) {
        ex.printStackTrace();
        tmpRetDetail.respCode = "-1";
        tmpRetDetail.respInfo = ex.getMessage();
        WriteBankLogLn(ex);
      }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsQryBatContract qryBatContract(ReqQryBatContract p_reqQry) throws Exception {
        return null;
    }
}
